﻿using RayD.Repository;
using System;
using System.Collections.Generic;
using System.Text;
using RayD.Repository.Entites.SystemModule;
using RayD.Core;
using Dapper;
using System.Data;
using System.Linq;

namespace RayD.App.SystemModule
{
    public class UserRelationApp
    {
        #region 构造函数注入
        private IRepository BaseRepository;
        public UserRelationApp(IRepository repository)
        {
            this.BaseRepository = repository;
        }
        #endregion

        #region 获取数据

        /// <summary>
        /// 获取用户Id-根据对象Id
        /// </summary>
        /// <returns></returns>
        [Caching]
        public virtual List<string> GetUserIdList(string B_ObjectId)
        {
            var expression = LinqExtensions.True<UserRelationEntity>();

            if (!B_ObjectId.IsEmpty())
            {
                expression = expression.And(c => c.B_ObjectId == B_ObjectId);
            }

            return BaseRepository.IQueryable(expression).Select(c => c.B_UserId).ToList();
        }
        /// <summary>
        /// 获取对象Id-根据用户Id & 对象分类
        /// </summary>
        /// <returns></returns>
        [Caching]
        public virtual List<string> GetObjIdList(string B_UserId, string B_Category)
        {
            var expression = LinqExtensions.True<UserRelationEntity>();

            if (!B_UserId.IsEmpty())
            {
                expression = expression.And(c => c.B_UserId == B_UserId);
            }
            if (!B_Category.IsEmpty())
            {
                expression = expression.And(c => c.B_Category == B_Category.ToInt());
            }

            return BaseRepository.IQueryable(expression).Select(c => c.B_ObjectId).ToList();
        }

        /// <summary>
        /// 获取对象Id-根据用户Id & 对象分类
        /// </summary>
        /// <param name="B_UserId"></param>
        /// <param name="B_Category"></param>
        /// <returns></returns>
        [Caching]
        public virtual string GetObjectIds(string B_UserId, string B_Category)
        {
            var expression = LinqExtensions.True<UserRelationEntity>();

            if (!B_UserId.IsEmpty())
            {
                expression = expression.And(c => c.B_UserId == B_UserId);
            }
            if (!B_Category.IsEmpty())
            {
                expression = expression.And(c => c.B_Category == B_Category.ToInt());
            }

            var list = BaseRepository.IQueryable(expression).Select(c => c.B_ObjectId);

            string res = "";
            foreach (var item in list)
            {
                if (res != "")
                {
                    res += ",";
                }
                res += item;
            }

            return res;
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存用户对应对象数据
        /// </summary>
        /// <param name="category">分类:1-角色2-岗位</param>
        /// <param name="objectId">对应对象主键</param>
        /// <param name="userIds">对应用户主键列表</param>
        /// <returns></returns>
        [RemoveCache]
        public virtual (bool, string) SaveForm(int category, string objectId, List<string> userIds)
        {
            var db = BaseRepository.BeginTrans();
            try
            {
                //构造集合
                List<UserRelationEntity> userRelationEntities = new List<UserRelationEntity>();
                for (int i = 0; i < userIds.Length(); i++)
                {
                    UserRelationEntity userRelationEntity = new UserRelationEntity()
                    {
                        B_Category = category,
                        B_ObjectId = objectId,
                        B_UserId = userIds[i],
                    };
                    userRelationEntity.Create();
                    userRelationEntities.Add(userRelationEntity);
                }
                //删除
                db.Delete<UserRelationEntity>(c => c.B_ObjectId.Equals(objectId));
                //新增
                db.Insert(userRelationEntities);
                db.Commit();
                return (true, "保存成功！");
            }
            catch (Exception)
            {
                db.Rollback();
                throw;
            }
        }

        #endregion

        #region 扩展操作

        #endregion

    }
}
